<template>
	<view>
		<view class="input" style="margin: 30rpx;">
			<uni-forms ref="form" :rules="{}" :model="user">
				<uni-forms-item label="姓名" name="">
					<uni-easyinput v-model="user.name" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item label="手机号" name="">
					<uni-easyinput v-model="user.telephone" placeholder="请输入手机号" />
				</uni-forms-item>
				<uni-forms-item label="密码" name="">
					<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
				</uni-forms-item>
		
				<uni-forms-item label="再次确认密码" name="">
					<uni-easyinput type="password" v-model="verify.password" placeholder="再次输入密码" />
				</uni-forms-item>
		
				<uni-forms-item label="学院" name="">
					<uni-combox label="" :candidates="verify.department" placeholder="请选择学院" v-model="user.department">
					</uni-combox>
				</uni-forms-item>
				<uni-forms-item label="专业" name="">
					<uni-combox label="" :candidates="verify.majors" placeholder="请选择专业" v-model="user.major">
					</uni-combox>
				</uni-forms-item>
				<uni-forms-item label="邮箱" name="">
					<uni-easyinput type="text" v-model="user.email" placeholder="请输入邮箱" />
				</uni-forms-item>
				<uni-forms-item label="" name="">
					<button type="primary" @click="register">注册</button>
				</uni-forms-item>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					name: '',
					password: '',
					telephone: '',
					department: '',
					email: '',
					major: '',
				},
				
				verify: {
					password: '', // 再次输入密码
					department: ['工学', '理学', '法学'],
					majors: ['计算机科学与技术', '自动化', '土木工程'],
				}
			}
		},
		methods: {
			register(e) {
				if (this.user.telephone && this.user.password !== '' &&
					this.user.password === this.verify.password
				) {
					uni.request({
						url: 'http://localhost:8090/freshmenapp/user/register',
						method: 'post',
						data: this.user,
						success: res => {
							console.log(res);
							if (res.data.success === true) {
								uni.showToast({
									title: this.user.name + '注册成功',
									icon: 'success'
								});
							} else {
								uni.showToast({
									title: '注册失败',
									icon: 'error'
								});
							}
						},
						fail: (err) => {
							uni.showToast({
								title: '网络错误',
								icon: 'error'
							});
						},
						complete: () => {}
					});
				} else {
					uni.showToast({
						title: '用户名或密码错误',
						icon: 'error'
					});
				}
			}
		}
	}
</script>

<style>

</style>
